﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MaxLeaf.Common;
using MaxLeaf.DB;
using System.Data.Common;
using System.Data;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;
using System.Transactions;
using System.Collections;

namespace MaxLeaf.DAL.W2K
{
    public class OeSalesOrderDAL
    {

        //特别注意****************************************************
        //在网站的数据库填的都是  K3 主帐套的 FItemID , 如果找不到，就应该
        //用fitemid找出主账套fnumber  再用fnumber到对应的K3分帐套支找。
        //************************************************************

        //备注： 是不是能通过 mary_order_info 中的mdc_ID 来获得region_dbName
        //SELECT * FROM  `mary_order_info`  A inner join mary_mdc B on A.mdc_id=B.id;
        //
        //同样由K3向网站写的时候，我们也可以用 FinterID + 数据库名称来判断是不是写到哪个网站数据库。

        //*****************************************************************************
        //select  ftypeid, * from  t_organization  where fnumber='1.11.001.001'
        //select  finterid, * from  t_SubMessage where ftypeid=501 and finterid=40001
        //这两个表的关系是： t_organization.ftypeid=t_SubMessage.finterid
        //*****************************************************************************

        //--导入销售单
        //geticbillno  -- 获得新的BillNo
        //getmaxnum --Finter
        public enum FromType
        {
            SEOrder,
            ICSale,
            ICStockBill,
            ICStockBillEntry
            ,SEOutStockChecked //发货通知单审核
            ,BlueICStockBillChecked//销售出库单审核
            ,RedICStockBillChecked//红字销售出库单审核
            ,RedOtherICStockBillChecked// 红字其他出库单审核
                , ICSaleChecked//销售发票核销
                  ,  ICSaleUnChecked//反核销
            , RedICStockBillDeleted//销售出库单删除
            , RedICSaleChecked//红字销售发票核销
        }
        public FromType mcurFromType = new FromType();

        private struct WebStatus
        {
            public string order_status;
            public string shipping_status;
            public string pay_status;
        }

        public enum OrderStatus
        {
            //0 未确认 1 已确认 2 已取消 3 待修改
            UnConfirm=0
            ,Confirmed
            ,Canceled
            ,Deleted
        }

        public enum ShipStatus
        {
            SEOutStockChecked=1 //发货通知单审核
            ,ICStockBillChecked=2//销售出库单审核
            
            ,RedChecked=4// 红字审核
        }
        public enum PayStatus
        {
            ICSaleChecked = 1//销售发票核销
            , ICSaleUnChecked = 0//	反核销
        }

        private enum WebStatusType
        {
            ICSales = 0, //销售发票审核
            CancelICSales = 1,//销售发票取消审核
            DelSEorder = 2,    //删除订单
        }

        private enum BillType //销售单据类别，  
        {
            /// <summary>
            /// 销售出库单据
            /// </summary>
            XOUT = 21,
            /// <summary>
            ///其他出库单据
            /// </summary>
            QOUT = 29,
            /// <summary>
            /// 销售订单
            /// </summary>
            SEORD = 81,
            /// <summary>
            /// 发货通知单据
            /// </summary>
            SEOUT = 83
        }
        private BillType mBillType = new BillType();
        public string mstrOEHDR_Web = "order_main";
        public string mstrOEHDR_K3 = "SEOrder";
        public string mstrOEDTL_Web = "order_goods";
        public string mstrOEDTL_K3 = "SEOrderEntry";
        //FheadselfS0152	网站订单号码
        //Fheadselfs0154	送货地址
        //Fheadselfs0155	收货人
        //FheadSelfS0156	备注
        //FheadSelfs0158	客户电话
        //FheadSelfS0159	配送方式
        //FheadSelfS0160	支付方式
        //FheadSelfS0161	发票类型
        //FheadSelfS0162	发票抬头
        //FheadSelfS0163	发票内容
        //FheadSelfS0164	订单生成时间

        public string mstrFieldNamesHDR_K3 = @" FBrNo,finterid,fbillno,FcustID,Fdate,FsaleStyle,FdeptID,FEmpID,Fcheckerid,fbillerid
                                                ,Ftrantype,finvoiceclosed,fbclosed,fmangerid,fsettleid,fexchangerate,fdiscounttype,fstatus,fcancellation
                                                ,Fcheckdate,Fsettledate,fseltrantype,fchildren,fbrid,fareaps,fsysstatus,Fcurrencyid,FDisCount,FDisCountRate,FCommitDisCount                                              
                                                ,FheadselfS0152,Fheadselfs0154,Fheadselfs0155,FheadSelfS0156
                                                ,FheadSelfs0158,FheadSelfS0159,FheadSelfS0160,FheadSelfS0161,FheadSelfS0162,FheadSelfS0163,FheadSelfS0164";

        public string mstrFieldNamesDTL_K3 = @"FBrNO,FInterID,FEntryID,FItemID,FQty,FCommitQty,FPrice,FAmount,FTaxRate,FTaxAmount,FTax,FDiscount,FNote,FDate,
                                                FDiscountAmount,FInvoiceQty,FBCommitQty,FTranLeadTime,FATPDeduct,FCostObjectID,FUnitID,FAuxPrice,FAuxQty,FCESS,
                                                FAllAmount,FAllStdAmount,FAuxPriceDiscount,FPriceDiscount,FTaxAmt,FAuxTaxPrice,FTaxPrice,FDetailID,FMrpAutoClosed,
                                                FSecStockQty,FSecInvoiceQty,FSecCommitInstall,FPlanMode,FEntrySelfS0164,FEntrySelfS0165,FAdviceConsignDate,FStockQty,FAuxStockQty,FMrpClosed,FBarCode";
        
        public string mstrFieldICStockBillDTL_K3 = @"FBrNO,FInterID,FEntryID,FItemID,FQty,FCommitQty,FPrice,FAmount,FTaxRate,FTaxAmount,FNote,
                                                FDiscountAmount,FUnitID,FAuxPrice,FAuxQty,
                                                FAuxTaxPrice,FDetailID,FDCSTockID,
                                                FSecInvoiceQty,FPlanMode,FConsignPrice,FConsignAmount,FBarCode,FentryselfB0949,FEntrySelfB0168";

        public string mstrFieldsICStockBillHDR_K3 = @"FBrNo,finterid,fbillno,Fcurrencyid,FSupplyID,Fdate,FsaleStyle,
                                                FdeptID,FEmpID,Fcheckerid,fbillerid,fmanagerid,FSmanagerID,
                                                FFmanagerID,Ftrantype,fstatus,fcancellation,
                                                Fcheckdate,Fsettledate,fseltrantype,fchildren,
                                                fbrid,FroB,FheadSelfB0939,FheadSelfB0941,FheadSelfB0942,
                                                FheadSelfB0943,FheadSelfB0945,FheadSelfB0946,FheadSelfB0947,FHeadSelfB0949,
                                FHeadSelfB0152,FHeadSelfB0154,FHeadSelfB0155,FHeadSelfB0156,FHeadSelfB0158,FHeadSelfB0159,
                                FHeadSelfB0160,FHeadSelfB0161,FHeadSelfB0162,FHeadSelfB0163,FHeadSelfB0164,FHeadSelfB0166";
        public struct OrderInfo
        {
            public string ServerName;
            public string DBName;
            public string BillNo_Web;
            public string BillNo_K3;
            public string FInterID;
            public string CustID;
            public DateTime FDate;
            public string CustTypeID;   //t_organization中的TypeID,和物料的FItemID一起决定，从icprcplyentry中取价格
            //public OrderDetail[] Detail;
            public List<OrderDetail> Detail;
        }
        public struct OrderDetail
        {
            /// <summary>
            /// 内部单号
            /// </summary>
            public string FInterID;

            /// <summary>
            /// 行号
            /// </summary>
            public string FEntryID;

            /// <summary>
            /// 物料ID
            /// </summary>
            public string FItemID;

            /// <summary>
            /// 物料类型ID，存在于t_icitem中，用于表示属于哪个类型，从而决定这个物料的折扣，价格表：icdisplyentry
            /// </summary>
            public string FItemTypeID;    // 物料类型ID，存在于t_icitem中，用于表示属于哪个类型，从而决定这个物料的折扣，价格表：icdisplyentry

            /// <summary>
            /// 基本单位的数量
            /// </summary>
            public double FBaseQty;

            /// <summary>
            /// 销售记量单位    
            /// </summary>
            public string FAuxUnitID;      //销售记量单位    

            /// <summary>
            /// 销售记量单位的价格
            /// </summary>
            public double FAuxPrice;       //销售记量单位的价格

            /// <summary>
            /// 销售记量单位的数量
            /// </summary>
            public double FAuxQty;       //销售记量单位的数量


            /// <summary>
            /// 基本记量单位 ,对应K3的 FUnitID
            /// </summary>
            public string FBaseUnitID;      //基本记量单位    

            /// <summary>
            /// 基本记量单位的价格, 对应K3的 FPrice
            /// </summary>
            public double FBasePrice;       //基本记量单位的价格

            /// <summary>
            /// 税率 
            /// </summary>
            public double FTaxRate;     //税率

            ///<summary>
            ///税率总金额   FBaseAmount * FTaxRate
            /// </summary>            
            public double FTaxAmount;


            /// <summary>
            /// 折扣率
            /// </summary>
            public double FDisRate;     // 折扣率

            /// <summary>
            /// 折扣金额  对应金蝶中的 FTaxAmount ，  出厂价*税率
            /// </summary>
            public double FDisAmt;     // 折扣金额

            /// <summary>
            /// 折扣价格  
            /// </summary>
            public double FDisPrice;     // 折扣价格

            /// <summary>
            /// 交货日期 
            /// </summary>
            public DateTime FDate;   //交货日期 

            /// <summary>
            /// 建议交货日期 
            /// </summary>
            public DateTime FAdviceConsignDate;   //建议交货日期 

            /// <summary>
            /// 扣税后的金额  FBaseQty * FBasePrice * (1- FDisRate*0.01)
            /// </summary>
            public double FBaseAmount;

            /// <summary>
            /// 出厂价格*单位Rate
            /// </summary>
            public double FAuxTaxPrice;

            /// <summary>
            /// 出厂价格
            /// </summary>
            public double FTaxPrice;

            /// <summary>
            /// 出厂单价 -- 0.72   ？？？？？？  是不是应该是金额来着，需要确认
            /// </summary>
            public double FEntrySelfS0161;

            /// <summary>
            /// 出厂实价 -- 0.72*(1-0.05) =0.684
            /// </summary>
            public double FEntrySelfS0162;

            /// <summary>
            /// 出厂金额 -- 4320 * 0.72   FQty*FEntrySelfS0161
            /// </summary>
            public double FEntrySelfS0163;

            /// <summary>
            /// goods_attr
            /// </summary>
            public string FentrySelfS0164;

            /// <summary>
            /// order_goods.order_goods_id
            /// </summary>
            public string FentrySelfS0165;

            /// <summary>
            /// 
            /// </summary>
            public double FCESS;

            /// <summary>
            /// 物料的颜色
            /// </summary>
            public string FEntrySelfS0168;

            /// <summary>
            /// 记录网站中行的rec_id
            /// </summary>
            public int FEntrySelfS0171;


            /// <summary>
            /// AuxUnit和BaseUnit之间的转换关系
            /// </summary>
            public double dblRate;

            /// <summary>
            /// 条形码，根据FitemID和FUnit来确定
            /// </summary>
            public string FBarCode;

        }

        public struct ICStockBillDetail
        {
            /// <summary>
            /// 内部单号
            /// </summary>
            public string FInterID;

            /// <summary>
            /// 行号
            /// </summary>
            public string FEntryID;

            /// <summary>
            /// 物料ID
            /// </summary>
            public string FItemID;

            /// <summary>
            ///  其他出库单据 (29) 时，需要填写
            /// </summary>
            public string FQtyMust;

            /// <summary>
            /// 基本单位的数量
            /// </summary>
            public double FQty;
            /// <summary>
            /// FUnit
            /// </summary>
            public string FBaseUnitID;      //基本记量单位    
            /// <summary>
            /// 基本记量单位的价格, 对应K3的 FPrice
            /// </summary>
            public double FBasePrice;       //基本记量单位的价格

            /// <summary>
            /// FAmount
            /// </summary>
            public double FBaseAmount;


            /// <summary>
            /// 税率 
            /// </summary>
            public double FTaxRate;     //税率

            public double FCommitQty;

            /// <summary>
            /// 销售记量单位的单价
            /// </summary>
            public double FAuxPrice;       //销售记量单位的单价

            /// <summary>
            /// 销售记量单位的数量
            /// </summary>
            public double FAuxQty;       //销售记量单位的数量

            /// <summary>
            /// 销售记量单位的数量 (29 时必须填写)
            /// </summary>
            public double FAuxQtyMust;       //销售记量单位的数量

            /// <summary>
            /// 销售价格
            /// </summary>
            public double FConsignPrice;
            /// <summary>
            /// 金额
            /// </summary>
            public double FConsignAmount;

            /// <summary>
            /// 记录网站中行的goods_attr
            /// </summary>
            public string FEntryself0949;

            /// <summary>
            /// 记录 AuxUnitID
            /// </summary>
            public string FAuxUnitID;

            /// <summary>
            /// AuxUnit和BaseUnit之间的转换关系
            /// </summary>
            public double dblRate;
          

            /// <summary>
            /// 14036
            /// </summary>
            public string FPlanMode;
            public string FBarCode;


            /// <summary>
            /// 如果是退货单，这个值就应该是-1。
            /// </summary>
            public double FSalesType;


            /// <summary>
            /// 发货仓库的FitemID
            /// </summary>
            public string FDCSTockID;

            /// <summary>
            /// 网站order_goods中的 order_goods_id
            /// </summary>
            public string FEntrySelfB0168;
        }
        private OrderInfo mcurOrder = new OrderInfo();
       
        private bool CheckServerAndDB(DBAccess dbWebCon, DBAccess dbCon, string strKeyValue)
        {
            string strDBName = dbCon.Connection.Database;
            string strSQL = @"select  k3_order_main.db_id,k3_db_main.dbname  from k3_order_main inner join k3_db_main on k3_order_main.db_id=k3_db_main.db_id and  k3_order_main.order_id='" + strKeyValue + "'";
            DataRow drCheckDB = dbWebCon.GetDataFirstRow(strSQL);
            if (drCheckDB == null)
            {
                LogManager.Debug(string.Format("k3_order_main数据有问题，请检查！"));
                return false;
            }
            if (CvtFunc.ToString(drCheckDB["dbname"]) != strDBName)
            {
                LogManager.Debug(string.Format("不是对应数据库！"));
                return false;
            }
            return true;
        }

        public void Delete(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbCon, string strKeyValue, OeSalesOrderDAL.FromType mFromType)
        {
            if (!CheckServerAndDB(dbOrgWebReadCon, dbCon, strKeyValue))
            {
                return;
            }
        }

        public void Add(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbCon, string strKeyValue, OeSalesOrderDAL.FromType mFromType)
        {
            if (!CheckServerAndDB(dbOrgWebReadCon, dbCon, strKeyValue))
            {
                return;
            }
            if (mFromType == OeSalesOrderDAL.FromType.SEOrder) 
            {
                if (!AddSales(dbOrgWebReadCon, dbOrgWebWriteCon, dbCon, strKeyValue))
                {
                    throw new MaxLeafException("添加销售单失败！");
                    return;
                }
            }
        }

        public void Update(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbCon, string strKeyValue, OeSalesOrderDAL.FromType mFromType)
        {
            if (!CheckServerAndDB(dbOrgWebReadCon, dbCon, strKeyValue))
            {
                return;
            }
            if (mFromType == OeSalesOrderDAL.FromType.SEOrder)
            {
                if (!UpdateSales(dbOrgWebReadCon, dbOrgWebWriteCon, dbCon, strKeyValue))
                {
                    throw new Exception("更新销售单失败！");
                    return;
                }
            }
        }


        private bool AddSales(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbCon, string strKeyValue)
        {

            string strSQL = "select * from " + mstrOEHDR_Web + " where order_id='" + strKeyValue + "'";
            DataRow drWebHdr = dbOrgWebReadCon.GetDataFirstRow(strSQL);
            if (drWebHdr == null) return false;
            if (CvtFunc.ToString(drWebHdr["Order_Type"]) == "1") //普通订单(order_type=1)产品传递至销售订单
            {
                mBillType = BillType.SEORD;
            }
            else if (CvtFunc.ToString(drWebHdr["Order_Type"]) == "2") //赠品订单(order_type=2)传递至蓝字其他出库单,金额和单价全部为0
            {
                mBillType = BillType.QOUT;
            }
            else if (CvtFunc.ToString(drWebHdr["Order_Type"]) == "0") //订单退货(order_type=0),传递至红字销售出库单
            {
                mBillType = BillType.XOUT;
            }
            else if (CvtFunc.ToString(drWebHdr["Order_Type"]) == "3") //赠品退货(order_type=3),传递至红字其他出库单
            {
                mBillType = BillType.QOUT;
            }

            if (mBillType == BillType.SEORD)
            {
                strSQL = "select finterid, fbillno from seorder where FheadselfS0152='" + CvtFunc.ToString(drWebHdr["order_sn"]) + "'";
            }
            else
            {
                strSQL = "select finterid, fbillno from icstockbill where FHeadSelfB0939='" + CvtFunc.ToString(drWebHdr["order_sn"]) + "'";
            }
            DataRow drCheck = dbCon.GetDataFirstRow(strSQL);
            if (drCheck != null)
            {
                dbOrgWebReadCon.ExecuteSql("delete from mLog where KeyValue='" + strKeyValue + "' and LogType='seorder'");
                throw new MaxLeafException("网站销售单" + CvtFunc.ToString(drWebHdr["order_sn"]) + " 已经产生过，请检查，其对应K3销售单号为：" + CvtFunc.ToString(drCheck["FbillNo"]));
            }
          

            if (CvtFunc.ToString(drWebHdr["Order_Type"]) == "1") //普通订单(order_type=1)产品传递至销售订单
            {
                if (AddSalesHdr(dbOrgWebReadCon, dbOrgWebWriteCon, dbCon, drWebHdr))
                {
                    if (AddSalesDetail(dbOrgWebReadCon, dbOrgWebWriteCon, dbCon, drWebHdr))
                    {
                        #region update k3_order_main
                        strSQL = "update k3_order_main set FItemID='" + mcurOrder.FInterID + "' where order_id='" + strKeyValue + "'";
                        dbOrgWebWriteCon.ExecuteSql(strSQL);
                        #endregion
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                   
                }
                else
                {
                    return false;
                }
            }
            //赠品订单(order_type=2)传递至蓝字其他出库单,金额和单价全部为0
            //订单退货(order_type=0),传递至红字销售出库单
            //赠品退货(order_type=3),传递至红字其他出库单
            if (CvtFunc.ToString(drWebHdr["Order_Type"]) == "0"
                || CvtFunc.ToString(drWebHdr["Order_Type"]) == "2"
                || CvtFunc.ToString(drWebHdr["Order_Type"]) == "3")
            {
                if (AddSalesICStockBill(dbOrgWebReadCon, dbOrgWebWriteCon, dbCon, drWebHdr))
                {
                    if (AddSalesICStockBillDetail(dbOrgWebReadCon, dbOrgWebWriteCon, dbCon, drWebHdr))
                    {
                        #region update k3_order_main
                        strSQL = "update k3_order_main set FItemID='" + mcurOrder.FInterID + "' where order_id='" + strKeyValue + "'";
                        dbOrgWebWriteCon.ExecuteSql(strSQL);
                        //if (CvtFunc.ToString(drWebHdr["Order_Type"]) == "0" || CvtFunc.ToString(drWebHdr["Order_Type"]) == "3")
                        //{
                        //    strSQL = "update k3_order_main set Ship_Status='3' where order_id='" + strKeyValue + "'";
                        //    dbOrgWebWriteCon.ExecuteSql(strSQL);
                        //}
                        #endregion
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
            return true;
        }

       

        private int intGetBillString(BillType billType)
        {
            if (billType == BillType.SEORD)
            {
                return 83;//"SEORD";
            }
            else if (billType == BillType.QOUT)
            {
                return 29; //"QOUT";
            }
            else if (billType == BillType.XOUT)
            {
                return 21;// "XOUT";
            }
            else if (billType == BillType.SEOUT)
            {
                return 83;// "SEOUT";
            }
            return 0;
        }

        private string  strGetBillString(BillType billType)
        {
            if (billType == BillType.SEORD)
            {
                return "SEORD";
            }
            else if (billType == BillType.QOUT)
            {
                return "QOUT";
            }
            else if (billType == BillType.XOUT)
            {
                return "XOUT";
            }
            else if (billType == BillType.SEOUT)
            {
                return "SEOUT";
            }
            return "";
        }

        private string GetBillNo(DBAccess dbCon, BillType billType)
        {
            int intType = intGetBillString (billType);
            string strBillNo = "";

            strBillNo = dbCon.GetValue("exec GetIcBillNo @IsSave,@FBillType,@BillId output"
                    , new SqlParameter[] {
                        new SqlParameter("@IsSave", "1")
                        ,new SqlParameter("@FBillType",intType)
                        ,new SqlParameter("@BillID",SqlDbType.VarChar,50)
                    }
                    , "BillID");

            string strMaxValue ="";
            if (billType== BillType.SEORD)
            {
                  strMaxValue = strBillNo.Substring(5, strBillNo.Length - 5);
            }
            else
            {
                  strMaxValue = strBillNo.Substring(4, strBillNo.Length - 4);
            }
            string strSQL = "";
            strBillNo = strGetBillString(billType)  + BaseFunctions.FormatWithZero(strMaxValue, 6);
            
            strSQL = "update ICBillNo set FcurNo='" + CvtFunc.ToInt(strMaxValue) + "', FDesc='" + strGetBillString(billType) + "+'+'" + BaseFunctions.FormatWithZero(strMaxValue, 6) + "' where FBillID='" + intType + "'";
            dbCon.ExecuteSql(strSQL);

            strSQL = "update t_BillCodeRule set FProjectVal = FProjectVal+1 where FBillTypeID='" + intType + "' and FClassIndex=2 and FProjectID=3";
            dbCon.ExecuteSql(strSQL);

            return strBillNo;
        }

        private string GetFinterID(DBAccess dbCon)
        {
            //GetICMaxNum
            // ALTER PROCEDURE [dbo].[GetICMaxNum]
            //@TableName VARCHAR (50),  
            //@FInterID  int output,    
            //@Increment INT = 1 ,   
            //@UserID    int =0 ---增加用户ID
            string strFinterID = "";
            string strTableName = "";
            if (mBillType == BillType.SEORD)
            {
                strTableName = "SEORDER";
            }            
            else
            {
                strTableName = "ICSTOCKBILL";
            }
            strFinterID = dbCon.GetValue("exec GetICMaxNum @TableName,@FInterID output,@Increment,@UserID"
                  , new SqlParameter[] {
                        new SqlParameter("@TableName", strTableName)
                        ,new SqlParameter("@FInterID",SqlDbType.Int)
                        ,new SqlParameter("@Increment","1")
                        ,new SqlParameter("@UserID","0")                        
                    }
                  , "FInterID");

            return strFinterID;
        }
       
        private bool AddSalesHdr(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbCon, DataRow drWebHdr)
        {
            string strCheckerName = "网站审核";
            ServerInfo K3Master = ServerInfo.GetK3MasterServer();
            using (DBAccess dbK3Master = new DBAccess(K3Master))
            {
                string strBillNo = GetBillNo(dbCon,BillType.SEORD);
                string strFinterID = GetFinterID(dbCon);
                string strSQL = "";
                //string strSQL = "select user_department,user_clerk from mary_users where user_id='" + drWebHdr["user_id"] + "'";
                //DataRow drMary_User = dbOrgWebReadCon.GetDataFirstRow(strSQL);

                strSQL ="select * from order_main where order_sn='"+ drWebHdr["order_sn"] +"'";
                DataRow drOrder_Main= dbOrgWebReadCon.GetDataFirstRow(strSQL );  
                if (drWebHdr != null)
                {
                    strSQL = "select top 0 * from seOrder";
                    DataRow drNewK3Hdr = dbCon.GetDataTable(strSQL).NewRow();
                    #region 添加到K3的表头
                    drNewK3Hdr["FBrNo"] = "0";
                    drNewK3Hdr["finterid"] = CvtFunc.ToInt(strFinterID);
                    drNewK3Hdr["fbillno"] = strBillNo;
                    drNewK3Hdr["Fcurrencyid"] = "1";
                    drNewK3Hdr["FcustID"] = dbOrgWebReadCon.GetValue("select FItemID from k3_user_main where user_id='" + CvtFunc.ToString(drOrder_Main["user_id"]) + "'");
                    drNewK3Hdr["Fdate"] = CvtFunc.ToDateTime(CvtFunc.ToLong(drOrder_Main["confirm_time"])).ToShortDateString();
                    drNewK3Hdr["FsaleStyle"] = (CvtFunc.ToString(drOrder_Main["pay_id"]) == "1" ? "100" : "101");
                    drNewK3Hdr["FDisCount"] = drOrder_Main["DiscAmt"];
                    drNewK3Hdr["FDisCountRate"] = drOrder_Main["DiscRate"];
                    drNewK3Hdr["FCommitDisCount"] = 0;

                    
                    //明天一定要提问k3_department中缺少department_id关键字 ， 和 k3_base_emp.department_id 中的联系不能键立   2011.12.11
                    strSQL ="SELECT k3_base_emp.FItemID as EmpFitemID,k3_department.FItemID as DepFItemID FROM  k3_base_emp ";
                    strSQL += " inner join order_main on  k3_base_emp.fitemid=order_main.emp_id and order_main.order_sn='" + drWebHdr["order_sn"] + "'";
                    strSQL += " inner join k3_department on k3_base_emp.department_id=k3_department.FItemID and order_main.order_sn='" + drWebHdr["order_sn"] + "'";
                    DataRow drEmp = dbOrgWebReadCon.GetDataFirstRow( strSQL );
                    if (drEmp != null)
                    {
                        drNewK3Hdr["FdeptID"] = CvtFunc.ToString(drEmp["DepFItemID"]);
                        drNewK3Hdr["FEmpID"] = CvtFunc.ToString(drEmp["EmpFitemID"]);
                    }
                    //网站过来的单子由审核人是“网站审核”
                    drNewK3Hdr["Fcheckerid"] =16405; //CvtFunc.ToInt(dbCon.GetValue("select FuserID from t_Base_User where fname='" + strCheckerName + "'"));
                    drNewK3Hdr["fbillerid"] = drNewK3Hdr["Fcheckerid"];
                    drNewK3Hdr["fmangerid"] = drNewK3Hdr["FEmpID"];
                    //}
                    drNewK3Hdr["Ftrantype"] = "81";
                    drNewK3Hdr["finvoiceclosed"] = "0";
                    drNewK3Hdr["fbclosed"] = "0";

                    drNewK3Hdr["fsettleid"] = "0";
                    drNewK3Hdr["fexchangerate"] = "1";
                    drNewK3Hdr["fdiscounttype"] = false;
                    drNewK3Hdr["fstatus"] = "1";
                    drNewK3Hdr["fcancellation"] = false;
                    drNewK3Hdr["Fcurrencyid"] = "1";
                    drNewK3Hdr["Fcheckdate"] = drNewK3Hdr["Fdate"];
                    drNewK3Hdr["Fsettledate"] = drNewK3Hdr["Fdate"];
                    drNewK3Hdr["fseltrantype"] = "0";
                    drNewK3Hdr["fchildren"] = "0";
                    //这两个字段要特殊取
                    drNewK3Hdr["fbrid"] = dbCon.GetValue("select top 1 fbrid from seorder");
                    drNewK3Hdr["fareaps"] = dbCon.GetValue("select top 1 fareaps from seorder");
                    //
                    drNewK3Hdr["fsysstatus"] = "2";
                    //drNewK3Hdr["fheadselfS0144"] = CvtFunc.ToString(drWebHdr["address"]);
                    //drNewK3Hdr["fheadselfS0160"] = CvtFunc.ToString(drWebHdr["shipping_name"]);
                    //drNewK3Hdr["Fheadselfs0154"] = CvtFunc.ToString(drWebHdr["address"]);
                    //drNewK3Hdr["fheadselfS0162"] = CvtFunc.ToString(drWebHdr["inv_type"]);
                    //drNewK3Hdr["fheadselfS0163"] = CvtFunc.ToString(drWebHdr["inv_payee"]);
                    //drNewK3Hdr["fheadselfS0164"] = CvtFunc.ToString(drWebHdr["inv_content"]);
                    //drNewK3Hdr["fheadselfS0165"] = CvtFunc.ToString(drWebHdr["consignee"]);
                    //drNewK3Hdr["fheadselfS0166"] = CvtFunc.ToString(drWebHdr["postscript"]);
                    //drNewK3Hdr["FheadselfS0152"] = CvtFunc.ToString(drWebHdr["order_sn"]);
                    //drNewK3Hdr["FHeadSelfS0150"] = CvtFunc.ToString(drWebHdr["pay_name"]);
                    //drNewK3Hdr["FHeadSelfS0168"] = DateTime.Now.ToString();

                    
                    drNewK3Hdr["FheadselfS0152"] = CvtFunc.ToString(drOrder_Main["order_sn"]);
                    drNewK3Hdr["Fheadselfs0154"] = CvtFunc.ToString(drOrder_Main["address"]);
                    drNewK3Hdr["Fheadselfs0155"] = CvtFunc.ToString(drOrder_Main["consignee"]);
                    drNewK3Hdr["FheadSelfS0156"] = CvtFunc.ToString(drOrder_Main["order_remark"]);
                    drNewK3Hdr["FheadSelfs0158"] = (CvtFunc.ToString(drOrder_Main["Tel"]) == "" ? "" : CvtFunc.ToString(drOrder_Main["Tel"])) + " " + (CvtFunc.ToString(drOrder_Main["Mobile"]) == "" ? "" : ", " + CvtFunc.ToString(drOrder_Main["Mobile"]));
                    drNewK3Hdr["FheadSelfS0159"] ="送货上门";
                    string strPay = "";
                    if (CvtFunc.ToString(drOrder_Main["pay_id"]) == "1")
                    {
                        strPay = "货到付款";
                    }
                    else
                    {
                        strPay = "月结";
                    }
                    drNewK3Hdr["FheadSelfS0160"] = strPay;
                    strSQL = "SELECT name FROM `order_main` inner join `user_info` on`order_main`.user_id=`user_info`.user_id inner join common_info on user_info.tax_type=common_info.common_info_id where order_main.user_id='" + CvtFunc.ToString(drOrder_Main["user_id"]) + "'";
                    drNewK3Hdr["FheadSelfS0161"] = dbOrgWebReadCon.GetValue(strSQL);  
                    drNewK3Hdr["FheadSelfS0162"] = CvtFunc.ToString(drOrder_Main["inv_payee"]);
                    drNewK3Hdr["FheadSelfS0163"] = CvtFunc.ToString(drOrder_Main["inv_content"]);
                    drNewK3Hdr["FheadSelfS0164"] = DateTime.Now.ToString();

                    #endregion
                    KeyValuePair<string, IDataParameter[]> kvp = dbCon.CreateParamSql(mstrFieldNamesHDR_K3, mstrOEHDR_K3, drNewK3Hdr, LogStatus.Add, "finterid");
                    dbCon.ExecuteSql(kvp.Key, kvp.Value);

                    #region Set mCurOrderInfo
                    mcurOrder.CustID = CvtFunc.ToString(drNewK3Hdr["FcustID"]);
                    mcurOrder.FInterID = strFinterID;
                    mcurOrder.BillNo_K3 = strBillNo;
                    mcurOrder.BillNo_Web = CvtFunc.ToString(drWebHdr["order_sn"]);
                    mcurOrder.CustTypeID = dbCon.GetValue("select ftypeid from t_organization where fitemid='" + mcurOrder.CustID + "'");
                    mcurOrder.FDate = CvtFunc.ToDateTime(drNewK3Hdr["FDate"]);
                    #endregion

                   
                }
                else
                {
                    return false;
                }
                return true;
            }
        }
        
        private bool AddSalesDetail(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbCon, DataRow drWebHdr)
        {
            ServerInfo server = ServerInfo.GetK3MasterServer();

            using (DBAccess dbK3Master = new DBAccess(server))
            {

                int intSeqNo = 1;
                string strKeyFieldName = "FinterID";
                DataRow drItem = null;
                string strSQL = "select * from " + mstrOEDTL_Web + " where order_id='" + drWebHdr["order_id"] + "' ";
                DataTable dtWebDetail = dbOrgWebReadCon.GetDataTable(strSQL);
                foreach (DataRow drWebDtl in dtWebDetail.Rows)
                {
                    OrderDetail newOrderDetail = new OrderDetail();
                    strSQL = "select top 0 * from " + mstrOEDTL_K3 + "";
                    DataRow drNewDtl_K3 = dbCon.GetDataTable(strSQL).NewRow();
                    #region 添加K3 的Detail表

                    newOrderDetail.FEntryID = intSeqNo.ToString();                    
                    strSQL = " SELECT B.FItemID FROM order_goods A inner join k3_goods_main B on A.goods_id=B.goods_id where A.goods_id= '" + CvtFunc.ToString(drWebDtl["goods_id"]) + "' ";
                    string strWebFitemID = dbOrgWebReadCon.GetValue(strSQL);
                    newOrderDetail.FItemID = new K2K.ImInvMasterDAL().GetDestMapKeyValue(dbK3Master, dbCon, strWebFitemID);
                    newOrderDetail.FInterID = mcurOrder.FInterID;
                    newOrderDetail.FAuxQty = CvtFunc.ToDouble(drWebDtl["Order_Unit_Number"]);
                    string strOrder_Unit_ID = new K2K.t_MeasureUnitDAL().GetDestMapKeyValue(dbK3Master, dbCon, CvtFunc.ToString(drWebDtl["order_unit_id"]));
                    strSQL = " select A.*,B.Fmeasureunitid from t_icItem A inner join t_measureunit B on A.FunitGroupID= B.FunitGroupID and ";
                    strSQL += " A.FItemID='" + newOrderDetail.FItemID + "' ";
                    strSQL += " and B.Fmeasureunitid='" + strOrder_Unit_ID + "'";
                    drItem = dbCon.GetDataFirstRow(strSQL);
                    if (drItem != null)
                    {
                        newOrderDetail.FBaseUnitID = CvtFunc.ToString(drItem["FUnitID"]);
                        newOrderDetail.FAuxUnitID = CvtFunc.ToString(drItem["Fmeasureunitid"]);
                        newOrderDetail.FTaxRate = CvtFunc.ToDouble(drItem["FTaxRate"]);   // 此物料的税率 
                        newOrderDetail.FCESS = newOrderDetail.FTaxRate;
                        newOrderDetail.FItemTypeID = CvtFunc.ToString(drItem["FTypeID"]); ;
                    }
                    else
                    {
                        throw new MaxLeafException(CvtFunc.ToString(drWebHdr["order_sn"]) + CvtFunc.ToString(drWebDtl["goods_name"]) + "  没有找到与之对应的 sales unit." + CvtFunc.ToString(drWebDtl["order_unit_name"]));
                        return false;  //没有找到与之对应的 sales unit.
                    }
                   
                        string strFunitID = "";
                        double dblFPrice = 0;


                        double dblRate = 1;
                        string strFunit = "";
                        double dblPrice = 0;
                        double dblPrice_ByCust = 0;
                        if (newOrderDetail.FBaseUnitID != newOrderDetail.FAuxUnitID)
                        {
                            strSQL = "select * from t_measureunit where FunitGroupID='{0}'and Fmeasureunitid='{1}'";
                            strSQL = string.Format(strSQL, CvtFunc.ToString(drItem["FunitGroupID"]), newOrderDetail.FAuxUnitID);
                            DataRow drRate = dbCon.GetDataFirstRow(strSQL);
                            if (drRate != null)
                            {
                                dblRate = CvtFunc.ToDouble(drRate["Fcoefficient"]);
                            }
                        }
                        newOrderDetail.dblRate = dblRate;
                        dblPrice = CvtFunc.ToDouble(drWebDtl["pmt_price"]);

                        if (newOrderDetail.FBaseUnitID == strFunit) // 取的是基本单位
                        {
                            newOrderDetail.FBasePrice = dblPrice / (1.00 + newOrderDetail.FTaxRate * 0.01);
                            newOrderDetail.FAuxPrice = dblRate * newOrderDetail.FBasePrice;
                            newOrderDetail.FAuxTaxPrice = dblPrice * dblRate;
                        }
                        else  //如果取的不是基本单位
                        {
                            newOrderDetail.FBasePrice = dblPrice / (1.00 + newOrderDetail.FTaxRate * 0.01) / dblRate;
                            newOrderDetail.FAuxPrice = dblRate * newOrderDetail.FBasePrice;
                            newOrderDetail.FAuxTaxPrice = dblPrice;
                        }

                        newOrderDetail.FDisRate = 0;
                        newOrderDetail.FBaseQty = newOrderDetail.FAuxQty * dblRate;
                        newOrderDetail.FBaseAmount = newOrderDetail.FBaseQty * newOrderDetail.FBasePrice * (1 - newOrderDetail.FDisRate * 0.01);

                        newOrderDetail.FTaxAmount = newOrderDetail.FBaseAmount * newOrderDetail.FTaxRate * 0.01;
                        newOrderDetail.FDisAmt = newOrderDetail.FBaseQty * dblPrice * newOrderDetail.FDisRate * 0.01;

                        if (newOrderDetail.FBaseUnitID == strFunit) // 取的是基本单位
                        {
                            newOrderDetail.FTaxPrice = dblPrice;
                            //newOrderDetail.FEntrySelfS0163 = newOrderDetail.FBaseQty * dblPrice / dblRate; //newOrderDetail.FEntrySelfS0161;
                            newOrderDetail.FDisPrice = dblPrice * (1 - newOrderDetail.FDisRate * 0.01) * dblRate;
                        }
                        else
                        {
                            newOrderDetail.FTaxPrice = dblPrice / dblRate;
                            //newOrderDetail.FEntrySelfS0163 = newOrderDetail.FAuxQty * dblPrice; //newOrderDetail.FEntrySelfS0161;
                            newOrderDetail.FDisPrice = dblPrice * (1 - newOrderDetail.FDisRate * 0.01);
                        }

                    

                        newOrderDetail.FEntrySelfS0161 = dblPrice;
                        newOrderDetail.FEntrySelfS0162 = dblPrice * (1 - newOrderDetail.FDisRate * 0.01);
                        
                        //newOrderDetail.FEntrySelfS0168 = CvtFunc.ToString(drWebDtl["goods_color"]);

                        newOrderDetail.FentrySelfS0164 = CvtFunc.ToString(drWebDtl["goods_attr"]);

                        newOrderDetail.FentrySelfS0165 = CvtFunc.ToString(drWebDtl["order_goods_id"]);
                    newOrderDetail.FAdviceConsignDate = mcurOrder.FDate;
                    newOrderDetail.FDate = mcurOrder.FDate;
                    strSQL  =" SELECT barcode FROM order_goods inner join goods_barcode";
                    strSQL +=" on order_goods.goods_id= goods_barcode.goods_id and order_unit_id=measure_unit_id and order_goods.order_goods_id ='"+ drWebDtl["order_goods_id"] +"'";
                    newOrderDetail.FBarCode = dbOrgWebReadCon.GetValue(strSQL);
                    #region 把newOrderDetail设置给DATAROW
                    if (!SetOrderDetailToDataRow(newOrderDetail, ref drNewDtl_K3))
                    {
                        throw new MaxLeafException(CvtFunc.ToString(drWebDtl["order_sn"]) + "赋值到DataRow中出错.添加销售单失败。");
                        return false;
                    }
                    #endregion
                    //mcurOrder.Detail.Add(newOrderDetail);  
                    KeyValuePair<string, IDataParameter[]> kvp = dbCon.CreateParamSql(mstrFieldNamesDTL_K3, mstrOEDTL_K3, drNewDtl_K3, LogStatus.Add, "FDetailID");
                    dbCon.ExecuteSql(kvp.Key, kvp.Value);

                    #endregion
                    intSeqNo++;
                }
                #region 把运输费用作为一行加入到Detail中  shipping_fee
                double dblShipFee = CvtFunc.ToDouble(drWebHdr["ship_fee"]);
                if (dblShipFee != 0)
                {
                    OrderDetail newOrderDetailFee = new OrderDetail();
                    strSQL = "select top 0 * from " + mstrOEDTL_K3 + "";
                    DataRow drNewDtlFee = dbCon.GetDataTable(strSQL).NewRow();
                    {
                        newOrderDetailFee.FItemID = dbCon.GetValue("select FItemID from t_icitem where f_103='1'");
                        strSQL = " select A.*,B.Fmeasureunitid from t_icItem A inner join t_measureunit B on A.FunitGroupID= B.FunitGroupID and ";
                        strSQL += " A.FItemID='" + newOrderDetailFee.FItemID + "' and A.f_103='1' ";
                        drItem = dbCon.GetDataFirstRow(strSQL);
                        if (drItem != null)
                        {
                            newOrderDetailFee.FBaseUnitID = CvtFunc.ToString(drItem["FUnitID"]);
                            newOrderDetailFee.FAuxUnitID = CvtFunc.ToString(drItem["Fmeasureunitid"]);
                            newOrderDetailFee.FTaxRate = CvtFunc.ToDouble(drItem["FTaxRate"]);   // 此物料的税率 
                            newOrderDetailFee.FCESS = newOrderDetailFee.FTaxRate;
                        }
                        else
                        {
                            throw new MaxLeafException(CvtFunc.ToString(drWebHdr["order_sn"]) + "添加运费失败。添加销售单失败。");
                            return false;
                        }
                        newOrderDetailFee.FInterID = mcurOrder.FInterID;
                        newOrderDetailFee.FDisRate = 0;
                        newOrderDetailFee.FEntryID = intSeqNo.ToString();
                        newOrderDetailFee.FBaseQty = 1;
                        double dblPrice = dblShipFee;
                        double dblRate = 1;
                        newOrderDetailFee.FBasePrice = dblPrice / (1.00 + newOrderDetailFee.FTaxRate * 0.01);
                        newOrderDetailFee.FAuxPrice = dblRate * newOrderDetailFee.FBasePrice;
                        newOrderDetailFee.FAuxQty = 1;
                        newOrderDetailFee.FBaseAmount = newOrderDetailFee.FBaseQty * newOrderDetailFee.FBasePrice * (1 - newOrderDetailFee.FDisRate * 0.01);

                        newOrderDetailFee.FTaxAmount = newOrderDetailFee.FBaseAmount * newOrderDetailFee.FTaxRate * 0.01;
                        newOrderDetailFee.FDisAmt = dblPrice * newOrderDetailFee.FDisRate * 0.01;

                        newOrderDetailFee.FAuxTaxPrice = dblPrice * dblRate;
                        newOrderDetailFee.FTaxPrice = dblPrice;
                        newOrderDetailFee.FDisPrice = dblPrice * (1 - newOrderDetailFee.FDisRate * 0.01);
                        newOrderDetailFee.FEntrySelfS0161 = dblPrice;
                        newOrderDetailFee.FEntrySelfS0162 = dblPrice * (1 - newOrderDetailFee.FDisRate * 0.01);
                        newOrderDetailFee.FEntrySelfS0163 = newOrderDetailFee.FBaseQty * newOrderDetailFee.FEntrySelfS0161;
                        newOrderDetailFee.FAdviceConsignDate = mcurOrder.FDate;
                        newOrderDetailFee.FDate = mcurOrder.FDate;
                        newOrderDetailFee.dblRate = 1;
                        newOrderDetailFee.FentrySelfS0165 = "999";  //这里给个999只是为了让这个行对应是最后一行。
                        newOrderDetailFee.FEntrySelfS0171 = 999;  //这里给个999只是为了让这个行对应是最后一行。
                        //mcurOrder.Detail.Add(newOrderDetailFee);
                        if (!SetOrderDetailToDataRow(newOrderDetailFee, ref drNewDtlFee))
                        {
                            throw new MaxLeafException(CvtFunc.ToString(drWebHdr["order_sn"]) + " 赋值到DataRow中出错.添加销售单失败。");
                            return false;
                        }
                        KeyValuePair<string, IDataParameter[]> kvp = dbCon.CreateParamSql(mstrFieldNamesDTL_K3, mstrOEDTL_K3, drNewDtlFee, LogStatus.Add, "FDetailID");
                        dbCon.ExecuteSql(kvp.Key, kvp.Value);
                    }
                }
                #endregion
                return true;
            }
        }


        #region ICStockBill
        /// <summary>
        /// 订单退货
        /// </summary>
        /// <param name="dbOrgWebReadCon"></param>
        /// <param name="dbOrgWebWriteCon"></param>
        /// <param name="dbCon"></param>
        /// <param name="drWebHdr"></param>
        /// <returns></returns>
        /// 
        
        private bool AddSalesICStockBill(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbCon, DataRow drWebHdr) 
        {
            string strCheckerName = "网站审核";
            ServerInfo K3Master = ServerInfo.GetK3MasterServer();
            using (DBAccess dbK3Master = new DBAccess(K3Master))
            {
                string strBillNo = "";

                strBillNo = GetBillNo(dbCon, mBillType);
                
                string strFinterID = GetFinterID(dbCon);
                string strSQL = "";
                //strSQL = "select user_department,user_clerk from mary_users where user_id='" + drWebHdr["user_id"] + "'";
                //DataRow drMary_User = dbOrgWebReadCon.GetDataFirstRow(strSQL);

                strSQL = "select * from order_main where order_sn='" + drWebHdr["order_sn"] + "'";
                DataRow drOrder_Main = dbOrgWebReadCon.GetDataFirstRow(strSQL);
                if (drWebHdr != null)
                {
                    strSQL = "select top 0 * from IcStockBill";
                    DataRow drNewK3Hdr = dbCon.GetDataTable(strSQL).NewRow();
                    #region 添加到K3的表头
                    drNewK3Hdr["FBrNo"] = "0";
                    drNewK3Hdr["finterid"] = CvtFunc.ToInt(strFinterID);
                    drNewK3Hdr["fbillno"] = strBillNo;
                    drNewK3Hdr["Fcurrencyid"] = "1";
                    drNewK3Hdr["FSupplyID"] = dbOrgWebReadCon.GetValue("select FItemID from k3_user_main where user_id='" + CvtFunc.ToString(drOrder_Main["user_id"]) + "'");
                    drNewK3Hdr["Fdate"] = CvtFunc.ToDateTime(CvtFunc.ToLong(drOrder_Main["confirm_time"])).ToShortDateString();
                    drNewK3Hdr["FsaleStyle"] = (CvtFunc.ToString(drOrder_Main["pay_id"]) == "1" ? "100" : "101");

                    //明天一定要提问k3_department中缺少department_id关键字 ， 和 k3_base_emp.department_id 中的联系不能键立   2011.12.11
                    strSQL = "SELECT k3_base_emp.FItemID as EmpFitemID,k3_department.FItemID as DepFItemID FROM  k3_base_emp ";
                    strSQL += " inner join order_main on k3_base_emp.fitemid=order_main.emp_id and order_main.order_sn='" + drWebHdr["order_sn"] + "'";
                    strSQL += " inner join k3_department on k3_base_emp.department_id=k3_department.fitemid and order_main.order_sn='" + drWebHdr["order_sn"] + "'";
                    DataRow drEmp = dbOrgWebReadCon.GetDataFirstRow(strSQL);
                    if (drEmp != null)
                    {
                        drNewK3Hdr["FdeptID"] = CvtFunc.ToString(drEmp["DepFItemID"]);
                        drNewK3Hdr["FEmpID"] = CvtFunc.ToString(drEmp["EmpFitemID"]);
                    }
                    //网站过来的单子由审核人是“网站审核”
                    //drNewK3Hdr["Fcheckerid"] = 16405; //CvtFunc.ToInt(dbCon.GetValue("select FuserID from t_Base_User where fname='" + strCheckerName + "'"));
                    drNewK3Hdr["fbillerid"] = 16405; //drNewK3Hdr["Fcheckerid"];
                    drNewK3Hdr["fmanagerid"] = drNewK3Hdr["Fcheckerid"];//drNewK3Hdr["FEmpID"];
                    drNewK3Hdr["FSmanagerID"] = drNewK3Hdr["FEmpID"];  //FSmanagerID  -- 保管
                    drNewK3Hdr["FFmanagerID"] = drNewK3Hdr["FEmpID"];  //FFmanagerid --  发货人
                    drNewK3Hdr["FCancellation"] = false ;
                    //}

                    //这个地方要确实是21(其他出库单据)还是29（销售出库单据）
                    drNewK3Hdr["Ftrantype"] = intGetBillString( mBillType);
                    //drNewK3Hdr["fstatus"] = "1";
                    drNewK3Hdr["fcancellation"] = false;
                    drNewK3Hdr["Fcurrencyid"] = "1";
                    //drNewK3Hdr["Fcheckdate"] = drNewK3Hdr["Fdate"];
                    drNewK3Hdr["Fsettledate"] = drNewK3Hdr["Fdate"];
                    drNewK3Hdr["fseltrantype"] = "0";
                    drNewK3Hdr["fchildren"] = "0";
                    //这两个字段要特殊取
                    drNewK3Hdr["fbrid"] = dbCon.GetValue("select top 1 fbrid from seorder");
                    drNewK3Hdr["FroB"] = (CvtFunc.ToString(drWebHdr["Order_Type"]) == "2" ? "1" : "-1") ; 
                    //drNewK3Hdr["fareaps"] = dbCon.GetValue("select top 1 fareaps from seorder");
                    //
                    //drNewK3Hdr["fsysstatus"] = "2";
                    //FheadSelfB0939	网站订单号	
                    //FheadSelfB0940	收货人	
                    //FheadSelfB0941	送货地址	
                    //FheadSelfB0945	客户电话	
                    //FheadSelfB0943	备注	
                    //FheadSelfB0946	配送方式	 '送货上门'
                    //FheadSelfB0947	支付方式	


                    drNewK3Hdr["FheadSelfB0939"] = CvtFunc.ToString(drOrder_Main["order_sn"]);
                    drNewK3Hdr["FheadSelfB0941"] = CvtFunc.ToString(drOrder_Main["address"]);
                    drNewK3Hdr["FheadSelfB0942"] = CvtFunc.ToString(drOrder_Main["consignee"]);
                    drNewK3Hdr["FheadSelfB0943"] = CvtFunc.ToString(drOrder_Main["order_remark"]);
                    drNewK3Hdr["FheadSelfB0945"] = (CvtFunc.ToString(drOrder_Main["Tel"]) == "" ? "" : CvtFunc.ToString(drOrder_Main["Tel"])) + " " + (CvtFunc.ToString(drOrder_Main["Mobile"]) == "" ? "" : ", " + CvtFunc.ToString(drOrder_Main["Mobile"]));
                    drNewK3Hdr["FheadSelfB0946"] = "送货上门";
                    string strPay = "";
                    if (CvtFunc.ToString(drOrder_Main["pay_id"]) == "1")
                    {
                        strPay = "货到付款";
                    }
                    else
                    {
                        strPay = "月结";
                    }
                    drNewK3Hdr["FheadSelfB0947"] = strPay;

                    if (CvtFunc.ToString(drWebHdr["order_type"]) == "0" || CvtFunc.ToString(drWebHdr["order_type"]) == "3")
                    {
                        drNewK3Hdr["FHeadSelfB0949"] = dbOrgWebReadCon.GetValue("select original_sn from order_main where order_sn='" + CvtFunc.ToString(drOrder_Main["order_sn"]) + "'");   //网站订单号
                    }
                    if ( CvtFunc.ToString ( drWebHdr["order_type"])=="0")
                    {
                        drNewK3Hdr["FHeadSelfB0152"] = dbOrgWebReadCon.GetValue("select original_sn from order_main where order_sn='" + CvtFunc.ToString(drOrder_Main["order_sn"]) + "'");   //网站订单号
                        drNewK3Hdr["FHeadSelfB0154"] = CvtFunc.ToString(drOrder_Main["address"]);  //送货地址
                        drNewK3Hdr["FHeadSelfB0155"]= CvtFunc.ToString(drOrder_Main["consignee"]);   //送货人
                        drNewK3Hdr["FHeadSelfB0156"]= CvtFunc.ToString(drOrder_Main["order_remark"]);  //备注
                        drNewK3Hdr["FHeadSelfB0158"]=(CvtFunc.ToString(drOrder_Main["Tel"]) == "" ? "" : CvtFunc.ToString(drOrder_Main["Tel"])) + " " + (CvtFunc.ToString(drOrder_Main["Mobile"]) == "" ? "" : ", " + CvtFunc.ToString(drOrder_Main["Mobile"]));    //客户电话
                        drNewK3Hdr["FHeadSelfB0159"] = "送货上门"; // 配送方式
                        drNewK3Hdr["FHeadSelfB0160"] = strPay;   //支付方式
                         // drNewK3Hdr["FHeadSelfB0161"]="";  //发票类型
                        drNewK3Hdr["FHeadSelfB0162"]=CvtFunc.ToString(drOrder_Main["inv_payee"]);   //发票抬头
                        drNewK3Hdr["FHeadSelfB0163"]=CvtFunc.ToString(drOrder_Main["inv_content"]);   //发票内容
                        drNewK3Hdr["FHeadSelfB0164"]=drNewK3Hdr["Fdate"];   //订货日期 
                        drNewK3Hdr["FHeadSelfB0166"] = CvtFunc.ToString(drOrder_Main["order_sn"]);  //网站退货单
                    }
                    //strSQL = "SELECT name FROM `order_main` inner join `user_info` on`order_main`.user_id=`user_info`.user_id inner join common_info on user_info.tax_type=common_info.common_info_id where user_id='" + CvtFunc.ToString(drOrder_Main["user_id"]) + "'";
                    //drNewK3Hdr["FheadSelfS0161"] = dbOrgWebReadCon.GetValue(strSQL);
                    //drNewK3Hdr["FheadSelfS0162"] = CvtFunc.ToString(drOrder_Main["inv_payee"]);
                    //drNewK3Hdr["FheadSelfS0163"] = CvtFunc.ToString(drOrder_Main["inv_content"]);
                    //drNewK3Hdr["FheadSelfS0164"] = DateTime.Now.ToString();

                    #endregion
                    KeyValuePair<string, IDataParameter[]> kvp = dbCon.CreateParamSql(mstrFieldsICStockBillHDR_K3, "ICStockBill", drNewK3Hdr, LogStatus.Add, "finterid");
                    dbCon.ExecuteSql(kvp.Key, kvp.Value);

                    #region Set mCurOrderInfo
                    mcurOrder.CustID = CvtFunc.ToString(drNewK3Hdr["FSupplyID"]);
                    mcurOrder.FInterID = strFinterID;
                    mcurOrder.BillNo_K3 = strBillNo;
                    mcurOrder.BillNo_Web = CvtFunc.ToString(drWebHdr["order_sn"]);
                    mcurOrder.CustTypeID = dbCon.GetValue("select ftypeid from t_organization where fitemid='" + mcurOrder.CustID + "'");
                    mcurOrder.FDate = CvtFunc.ToDateTime(drNewK3Hdr["FDate"]);
                    #endregion

               
                }
                else
                {
                    return false;
                }
                return true;
            }
        }

        private bool AddSalesICStockBillDetail(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbCon, DataRow drWebHdr)
        {
            ServerInfo server = ServerInfo.GetK3MasterServer();

            using (DBAccess dbK3Master = new DBAccess(server))
            {
                int intSeqNo = 1;
                string strKeyFieldName = "FinterID";
                DataRow drItem = null;
                string strSQL = "select * from order_goods where order_id='" + drWebHdr["order_id"] + "' ";
                DataTable dtWebDetail = dbOrgWebReadCon.GetDataTable(strSQL);
                foreach (DataRow drWebDtl in dtWebDetail.Rows)
                {
                    ICStockBillDetail newOrderDetail = new ICStockBillDetail();
                    strSQL = "select top 0 * from ICStockBillEntry";
                    DataRow drNewDtl_K3 = dbCon.GetDataTable(strSQL).NewRow();
                    #region 添加K3 的Detail表
                    newOrderDetail.FSalesType = (CvtFunc.ToString(drWebHdr["order_type"]) == "0" || CvtFunc.ToString(drWebHdr["order_type"]) == "3" ? -1 : 1);
                    newOrderDetail.FEntryID = intSeqNo.ToString();
                    newOrderDetail.FEntrySelfB0168 = CvtFunc.ToString(drWebDtl["order_goods_id"]);
                    strSQL = " SELECT B.FItemID FROM order_goods A inner join k3_goods_main B on A.goods_id=B.goods_id where A.goods_id= '" + CvtFunc.ToString(drWebDtl["goods_id"]) + "' ";
                    string strWebFitemID = dbOrgWebReadCon.GetValue(strSQL);
                    newOrderDetail.FItemID = new K2K.ImInvMasterDAL().GetDestMapKeyValue(dbK3Master, dbCon, strWebFitemID);
                    string strOrder_Unit_ID = new K2K.t_MeasureUnitDAL().GetDestMapKeyValue(dbK3Master, dbCon, CvtFunc.ToString(drWebDtl["order_unit_id"]));
                    newOrderDetail.FInterID = mcurOrder.FInterID;
                    newOrderDetail.FAuxQty = CvtFunc.ToDouble(drWebDtl["Order_Unit_Number"]);
                    newOrderDetail.FQty = CvtFunc.ToDouble(drWebDtl["Basic_Unit_Number"]);
                    strSQL = " select A.*,B.Fmeasureunitid from t_icItem A inner join t_measureunit B on A.FunitGroupID= B.FunitGroupID and ";
                    strSQL += " A.FItemID='" + newOrderDetail.FItemID + "' ";
                    strSQL += " and B.Fmeasureunitid='" + strOrder_Unit_ID + "'";
                    drItem = dbCon.GetDataFirstRow(strSQL);
                    if (drItem != null)
                    {
                        newOrderDetail.FBaseUnitID = CvtFunc.ToString(drItem["FUnitID"]);
                        newOrderDetail.FAuxUnitID = CvtFunc.ToString(drItem["Fmeasureunitid"]);
                        newOrderDetail.FTaxRate = CvtFunc.ToDouble(drItem["FTaxRate"]);   // 此物料的税率 
                    }
                    else
                    {
                        throw new MaxLeafException(CvtFunc.ToString(drWebHdr["order_sn"]) + CvtFunc.ToString(drWebDtl["goods_name"]) + "  没有找到与之对应的 sales unit." + CvtFunc.ToString(drWebDtl["order_unit_name"]));
                        return false;  //没有找到与之对应的 sales unit.
                    }

                    string strFunitID = "";
                    double dblFPrice = 0;


                    double dblRate = 1;
                    string strFunit = "";
                    double dblPrice = 0;
                  
                    if (newOrderDetail.FBaseUnitID != newOrderDetail.FAuxUnitID)
                    {
                        strSQL = "select * from t_measureunit where FunitGroupID='{0}'and Fmeasureunitid='{1}'";
                        strSQL = string.Format(strSQL, CvtFunc.ToString(drItem["FunitGroupID"]), newOrderDetail.FAuxUnitID);
                        DataRow drRate = dbCon.GetDataFirstRow(strSQL);
                        if (drRate != null)
                        {
                            dblRate = CvtFunc.ToDouble(drRate["Fcoefficient"]);
                        }
                    }
                    newOrderDetail.dblRate = dblRate;
                    dblPrice = CvtFunc.ToDouble(drWebDtl["pmt_price"]);
                    
                    newOrderDetail.FEntryself0949 = CvtFunc.ToString(drWebDtl["goods_attr"]);

                    newOrderDetail.FBasePrice = dblPrice / (1.00 + newOrderDetail.FTaxRate * 0.01) / dblRate;
                    newOrderDetail.FAuxPrice = dblPrice; //dblRate * newOrderDetail.FBasePrice;
                    newOrderDetail.FBaseAmount = newOrderDetail.FAuxPrice * newOrderDetail.FQty;

                    string strMDCFItemID = dbOrgWebReadCon.GetValue("select k3_region_mdc.fitemid from order_main inner join k3_region_mdc on order_main.mdc_id=k3_region_mdc.region_mdc_id and k3_region_mdc.region_type='4' and order_main.order_id='" + drWebHdr["order_id"] + "'");
                    string strMDCFnumber = dbCon.GetValue("select FFullnumber from t_item where fitemid='" + strMDCFItemID + "' and FItemClassID='1'");
                    newOrderDetail.FDCSTockID = dbCon.GetValue("select fitemid from t_item where FFullnumber='" + strMDCFnumber + "' and FItemClassID='5'");
                    if (newOrderDetail.FDCSTockID == "")
                    {
                        throw new MaxLeafException(CvtFunc.ToString(drWebHdr["order_sn"]) + " 没有找到对应的仓库！");
                    }

                    //newOrderDetail.FAuxTaxPrice = dblPrice;
                  
                    strSQL = " SELECT barcode FROM order_goods inner join goods_barcode";
                    strSQL += " on order_goods.goods_id= goods_barcode.goods_id and order_unit_id=measure_unit_id and order_goods.order_goods_id ='" + drWebDtl["order_goods_id"] + "'";
                    newOrderDetail.FBarCode = dbOrgWebReadCon.GetValue(strSQL);

                    #region 把newOrderDetail设置给DATAROW
                    if (!SetICStockBillEntryToDataRow(newOrderDetail, ref drNewDtl_K3))
                    {
                        throw new MaxLeafException(CvtFunc.ToString(drWebDtl["order_sn"]) + "赋值到DataRow中出错.添加销售单失败。");
                        return false;
                    }
                    #endregion
                    //mcurOrder.Detail.Add(newOrderDetail);  
                    KeyValuePair<string, IDataParameter[]> kvp = dbCon.CreateParamSql(mstrFieldICStockBillDTL_K3, "ICStockBillEntry", drNewDtl_K3, LogStatus.Add, "FDetailID");
                    dbCon.ExecuteSql(kvp.Key, kvp.Value);

                    #endregion
                    intSeqNo++;
                }
                #region 把运输费用作为一行加入到Detail中  shipping_fee
                double dblShipFee = CvtFunc.ToDouble(drWebHdr["ship_fee"]);
                if (dblShipFee != 0)
                {
                    ICStockBillDetail newOrderDetailFee = new ICStockBillDetail();
                    strSQL = "select top 0 * from ICStockBillEntry";
                    DataRow drNewDtlFee = dbCon.GetDataTable(strSQL).NewRow();
                    {
                        newOrderDetailFee.FItemID = dbCon.GetValue("select FItemID from t_icitem where f_103='1'");
                        strSQL = " select A.*,B.Fmeasureunitid from t_icItem A inner join t_measureunit B on A.FunitGroupID= B.FunitGroupID and ";
                        strSQL += " A.FItemID='" + newOrderDetailFee.FItemID + "' and A.f_103='1' ";
                        drItem = dbCon.GetDataFirstRow(strSQL);
                        if (drItem != null)
                        {
                            newOrderDetailFee.FBaseUnitID = CvtFunc.ToString(drItem["FUnitID"]);
                            newOrderDetailFee.FAuxUnitID = CvtFunc.ToString(drItem["Fmeasureunitid"]);
                            newOrderDetailFee.FTaxRate = CvtFunc.ToDouble(drItem["FTaxRate"]);   // 此物料的税率                             
                        }
                        else
                        {
                            throw new MaxLeafException(CvtFunc.ToString(drWebHdr["order_sn"]) + "添加运费失败。添加销售单失败。");
                            return false;
                        }
                        newOrderDetailFee.FInterID = mcurOrder.FInterID;                        
                        newOrderDetailFee.FEntryID = intSeqNo.ToString();
                        newOrderDetailFee.FQty = 1;
                        newOrderDetailFee.FSalesType = (CvtFunc.ToString(drWebHdr["order_type"]) == "0" || CvtFunc.ToString(drWebHdr["order_type"]) == "3" ? -1 : 1);
                        double dblPrice = dblShipFee;
                        double dblRate = 1;
                        newOrderDetailFee.FBasePrice = dblPrice / (1.00 + newOrderDetailFee.FTaxRate * 0.01);
                        newOrderDetailFee.FAuxPrice = dblPrice;
                        newOrderDetailFee.FAuxQty = 1;
                        newOrderDetailFee.FBaseAmount = newOrderDetailFee.FAuxQty * newOrderDetailFee.FAuxPrice;

                        string strMDCFItemID = dbOrgWebReadCon.GetValue("select k3_region_mdc.fitemid from order_main inner join k3_region_mdc on order_main.mdc_id=k3_region_mdc.region_mdc_id and k3_region_mdc.region_type='4' and order_main.order_id='" + drWebHdr["order_id"] + "'");
                        string strMDCFnumber = dbCon.GetValue("select FFullnumber from t_item where fitemid='" + strMDCFItemID + "' and FItemClassID='1'");
                        newOrderDetailFee.FDCSTockID = dbCon.GetValue("select fitemid from t_item where FFullnumber='" + strMDCFnumber + "' and FItemClassID='5'");
                        if (newOrderDetailFee.FDCSTockID == "")
                        {
                            throw new MaxLeafException(CvtFunc.ToString(drWebHdr["order_sn"]) + " 没有找到对应的仓库！");
                        }

                        newOrderDetailFee.dblRate = 1;
                        if (!SetICStockBillEntryToDataRow(newOrderDetailFee, ref drNewDtlFee))
                        {
                            throw new MaxLeafException(CvtFunc.ToString(drWebHdr["order_sn"]) + " 赋值到DataRow中出错.添加销售单失败。");
                            return false;
                        }
                        KeyValuePair<string, IDataParameter[]> kvp = dbCon.CreateParamSql(mstrFieldICStockBillDTL_K3, "ICStockBillEntry", drNewDtlFee, LogStatus.Add, "FDetailID");
                        dbCon.ExecuteSql(kvp.Key, kvp.Value);
                    }
                }
                #endregion
                return true;
            }
        }
        
        
        #endregion

        /// <summary>
        /// SEOrderEntry
        /// </summary>
        /// <param name="newOrderDetail"></param>
        /// <param name="drNewDtl_K3"></param>
        /// <returns></returns>
        private bool SetOrderDetailToDataRow(OrderDetail newOrderDetail, ref DataRow drNewDtl_K3)
        {
            drNewDtl_K3["FBrNO"] = "0";
            drNewDtl_K3["FInterID"] = CvtFunc.ToInt(newOrderDetail.FInterID);
            drNewDtl_K3["FEntryID"] = CvtFunc.ToInt(newOrderDetail.FEntryID);
            drNewDtl_K3["FItemID"] = CvtFunc.ToInt(newOrderDetail.FItemID);
            drNewDtl_K3["FQty"] = newOrderDetail.FBaseQty;
            drNewDtl_K3["FCommitQty"] = "0";
            drNewDtl_K3["FPrice"] = newOrderDetail.FBasePrice;
            drNewDtl_K3["FAmount"] = newOrderDetail.FBaseAmount;
            drNewDtl_K3["FTaxRate"] = newOrderDetail.FDisRate;
            drNewDtl_K3["FTaxAmount"] = newOrderDetail.FAuxQty * newOrderDetail.FAuxTaxPrice * newOrderDetail.FDisRate * 0.01;  //折扣金额
            drNewDtl_K3["FTax"] = "0";
            drNewDtl_K3["FDiscount"] = "0";
            drNewDtl_K3["FNote"] = "";
            drNewDtl_K3["FDate"] = newOrderDetail.FDate;
            drNewDtl_K3["FAdviceConsignDate"] = newOrderDetail.FAdviceConsignDate;
            drNewDtl_K3["FDiscountAmount"] = newOrderDetail.FTaxAmount;
            drNewDtl_K3["FInvoiceQty"] = "0";
            drNewDtl_K3["FBCommitQty"] = "0";
            drNewDtl_K3["FTranLeadTime"] = "0";
            drNewDtl_K3["FATPDeduct"] = "0";
            drNewDtl_K3["FCostObjectID"] = "0";
            drNewDtl_K3["FUnitID"] = newOrderDetail.FAuxUnitID;
            drNewDtl_K3["FAuxPrice"] = newOrderDetail.FAuxPrice;
            drNewDtl_K3["FAuxQty"] = newOrderDetail.FAuxQty;
            drNewDtl_K3["FCESS"] = newOrderDetail.FCESS;
            drNewDtl_K3["FAllAmount"] = newOrderDetail.FDisPrice * newOrderDetail.FAuxQty;
            drNewDtl_K3["FAllStdAmount"] = drNewDtl_K3["FAllAmount"];
            drNewDtl_K3["FTaxAmt"] = newOrderDetail.FTaxAmount;
            drNewDtl_K3["FAuxPriceDiscount"] = newOrderDetail.FDisPrice;//newOrderDetail.FAuxTaxPrice;
            drNewDtl_K3["FPriceDiscount"] = newOrderDetail.FDisPrice / newOrderDetail.dblRate;

            drNewDtl_K3["FAuxTaxPrice"] = newOrderDetail.FAuxTaxPrice;
            drNewDtl_K3["FTaxPrice"] = newOrderDetail.FTaxPrice;
            //drNewDtl_K3["FDetailID,
            //rNewDtl_K3["FMrpAutoClosed,
            //drNewDtl_K3["FSecStockQty,
            //drNewDtl_K3["FSecInvoiceQty,
            //drNewDtl_K3["FSecCommitInstall,

            drNewDtl_K3["FStockQty"] = 0; //newOrderDetail.FBaseQty;
            drNewDtl_K3["FAuxStockQty"] = 0; // newOrderDetail.FAuxQty;
            //drNewDtl_K3["FcommitQty"] = newOrderDetail.FBaseQty; 
            drNewDtl_K3["FPlanMode"] = "14036";
            drNewDtl_K3["FbarCode"] = newOrderDetail.FBarCode;
            //drNewDtl_K3["FEntrySelfS0161"] = newOrderDetail.FEntrySelfS0161 / newOrderDetail.dblRate;
            //drNewDtl_K3["FEntrySelfS0162"] = newOrderDetail.FEntrySelfS0162 / newOrderDetail.dblRate;
            //drNewDtl_K3["FEntrySelfS0163"] = newOrderDetail.FEntrySelfS0163;

            //drNewDtl_K3["FEntrySelfS0168"] = newOrderDetail.FEntrySelfS0168;
            //drNewDtl_K3["FEntrySelfS0171"] = newOrderDetail.FEntrySelfS0171;
            drNewDtl_K3["FEntrySelfS0164"] = newOrderDetail.FentrySelfS0164; 
            drNewDtl_K3["FEntrySelfS0165"] = newOrderDetail.FentrySelfS0165; 
            drNewDtl_K3["FMrpClosed"] = 0;
            return true;
        }

        /// <summary>
        /// ICStockBillEntry
        /// </summary>
        /// <param name="newOrderDetail"></param>
        /// <param name="drNewDtl_K3"></param>
        /// <returns></returns>
        private bool SetICStockBillEntryToDataRow(ICStockBillDetail newOrderDetail, ref DataRow drNewDtl_K3)
        {
            drNewDtl_K3["FBrNO"] = "0";
            drNewDtl_K3["FInterID"] = CvtFunc.ToInt(newOrderDetail.FInterID);
            drNewDtl_K3["FEntryID"] = CvtFunc.ToInt(newOrderDetail.FEntryID);
            drNewDtl_K3["FItemID"] = CvtFunc.ToInt(newOrderDetail.FItemID);
            drNewDtl_K3["FQty"] = newOrderDetail.FSalesType * newOrderDetail.FQty;
            drNewDtl_K3["FCommitQty"] = "0";
            drNewDtl_K3["FPrice"] = newOrderDetail.FBasePrice;
            drNewDtl_K3["FAmount"] = newOrderDetail.FSalesType * newOrderDetail.FBaseAmount;           

            drNewDtl_K3["FConsignPrice"]= newOrderDetail.FAuxPrice ;
            drNewDtl_K3["FConsignAmount"] = -1 * newOrderDetail.FAuxPrice * newOrderDetail.FAuxQty;
            drNewDtl_K3["FTaxAmount"] = 0;//newOrderDetail.FAuxQty * newOrderDetail.FAuxPrice * 0.01;  //折扣金额
            drNewDtl_K3["FNote"] = "";
            drNewDtl_K3["FDCSTockID"] = newOrderDetail.FDCSTockID;

            //drNewDtl_K3["FInvoiceQty"] = "0";
            //drNewDtl_K3["FBCommitQty"] = "0";
            //drNewDtl_K3["FTranLeadTime"] = "0";
            //drNewDtl_K3["FATPDeduct"] = "0";
            //drNewDtl_K3["FCostObjectID"] = "0";
            drNewDtl_K3["FUnitID"] = newOrderDetail.FAuxUnitID;
            drNewDtl_K3["FAuxPrice"] = newOrderDetail.FAuxPrice;
            drNewDtl_K3["FAuxQty"] = newOrderDetail.FSalesType * newOrderDetail.FAuxQty;


            drNewDtl_K3["FAuxTaxPrice"] = 0 ; //newOrderDetail.FAuxTaxPrice;
            drNewDtl_K3["FPlanMode"] = "14036";
           
            drNewDtl_K3["FBarCode"]= newOrderDetail.FBarCode ;
            drNewDtl_K3["FentryselfB0949"] = newOrderDetail.FEntryself0949; 
            //drNewDtl_K3["FMrpClosed"] = 0;
            return true;
        }


        private bool UpdateSalesDetail(OrderDetail newOrderDetail, LogStatus updateMode)
        {

            return true;
        }

        private bool UpdateSales(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbCon, string strKeyValue)
        {
            string strSQL = "select * from " + mstrOEHDR_Web + " where order_sn='" + strKeyValue + "'";
            DataRow drWebHdr = dbOrgWebReadCon.GetDataFirstRow(strSQL);
            if (DelDetail(dbOrgWebReadCon, dbCon, drWebHdr))
            {
                if (AddSalesDetail(dbOrgWebReadCon, dbOrgWebWriteCon, dbCon, drWebHdr))
                {

                }
                else
                {
                    throw new MaxLeafException(strKeyValue + " 添加销售单失败.");
                    return false;
                }
            }
            else
            {
                return false;
            }
            return true;
        }

        private bool DelDetail(DBAccess dbWebCon, DBAccess dbCon, DataRow drWebHdr)
        {
            return true;
        }
    }
}
